package com.study.order.proxy.cglib;

import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
import java.lang.reflect.UndeclaredThrowableException;

/**
 * @author yuanqiji
 * @date 2022/08/16
 **/
public class Proxy extends Target {
	private MethodInterceptor methodInterceptor;

	public void setMethodInterceptor(MethodInterceptor methodInterceptor) {
		this.methodInterceptor = methodInterceptor;
	}


	//--------------------------->带原始功能的方法

	static MethodProxy save0Proxy;
	static MethodProxy save1Proxy;
	static MethodProxy save2Proxy;

	public void saveSuper() {
		super.save();
	}

	public void saveSuper(int i) {
		super.save(i);
	}

	public void saveSuper(long i) {
		super.save(i);
	}


	static Method save0;
	static Method save1;
	static Method save2;

	static {
		try {
			save0 = Target.class.getMethod("save");
			save1 = Target.class.getMethod("save", int.class);
			save2 = Target.class.getMethod("save", long.class);
			save0Proxy = MethodProxy.create(Target.class, Proxy.class, "()V", "save", "saveProxy ");
			save1Proxy = MethodProxy.create(Target.class, Proxy.class, "(I)V", "save", "saveProxy ");
			save2Proxy = MethodProxy.create(Target.class, Proxy.class, "(J)V", "save", "saveProxy ");
		} catch (NoSuchMethodException e) {
			throw new NoSuchMethodError(e.getMessage());
		}

	}


	//--------------------------->带增强功能的方法
	@Override
	public void save() {
		try {
			methodInterceptor.intercept(this, save0, new Object[0], save0Proxy);
		} catch (Throwable e) {
			throw new UndeclaredThrowableException(e);
		}
	}

	@Override
	public void save(int i) {
		try {
			methodInterceptor.intercept(this, save1, new Object[]{i}, save1Proxy);
		} catch (Throwable e) {
			throw new UndeclaredThrowableException(e);
		}
	}

	@Override
	public void save(long i) {
		try {
			methodInterceptor.intercept(this, save2, new Object[]{i}, save2Proxy);
		} catch (Throwable e) {
			throw new UndeclaredThrowableException(e);
		}
	}
}
